package com.niu.business.aop;

import com.niu.business.annotation.MyLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class AopMyLog {

    @Pointcut("@annotation(com.niu.business.annotation.MyLog)")
    public void onlyLog() {

    }

    @Around("onlyLog()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        String className = joinPoint.getTarget().getClass().getName();//获取调用的名字
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        MyLog myLog = method.getAnnotation(MyLog.class);

        System.out.println("调用的方法名是" + className);
        System.out.println("自定义注解上的值是" + myLog.value());
        return result;
    }
}
